#include "drv_key.h"
#include "SC_Init.h"
#include "stdio.h"
#include "string.h"
#include "SMProcess.h"
#include "..\Drivers\SCDriver_list.h"
#include "filterCounter.h"
#define	 LongPress		3000
#define	 ShortPress		70
#define	 keyLoneStep	0x0F

bool xdata keyTickFlag;
// extern EVT_STATUS evt_Filter;
extern uint32_t xdata eventValue;
extern uint32_t xdata keyTick;

enum KEY
{
	/*IonKey=0x10,
	PowerKey=0x20,
	ModeKey=0x40,
	TimingKey=0x80,
	AmdKey=0x100,
	ModeLoneKey=ModeKey+keyLoneStep,
	*/
	PowerKey=0x20,
	AmdKey=0x40,
	ModeKey=0x80,
	IonKey=0x100,
	ModeLoneKey =ModeKey+keyLoneStep,
	FilterSpeedOutKey = (AmdKey|IonKey)+keyLoneStep,
	FactoryTestKey = (ModeKey|IonKey)+keyLoneStep,
	MatchNetworkKey = (ModeKey|AmdKey)+keyLoneStep,
};

typedef enum{
	NONE_PRESS=0,
	PRESS_DOWN,
	PRESS_UP,
	SHORT_PRESS_START,
	LONG_PRESS_START,
}KEY_STATE;

bool keyTick_ONOFF(void)
{
	return keyTickFlag;
}
uint32_t getkeyTick(void)
{
	return keyTick;
}
void clearkeyTick(void)
{
	keyTick=0;
}

uint32_t getTouchKeyValue(uint32_t exKeyValueFlag)
{
		static KEY_STATE 	keyState = NONE_PRESS;
		static uint32_t xdata lastKeyValue=0;
		uint32_t xdata tempKeyValue=0;
		switch(keyState)
		{
				case NONE_PRESS:
					if(exKeyValueFlag!=0x00)
					{
						keyTickFlag=TRUE;
						lastKeyValue=exKeyValueFlag;
						keyState=PRESS_DOWN;
					}
					else
					{
						keyState=NONE_PRESS;
						lastKeyValue=0;
					}		
				break;
					
				case PRESS_DOWN:
					if(exKeyValueFlag != lastKeyValue )
					{
							keyState=PRESS_UP;
							keyTickFlag=FALSE;	
					}
					else
					{
						if(getkeyTick() > LongPress)
						{
							keyState=LONG_PRESS_START;
							keyTickFlag=FALSE;
						}
						else
						{
							keyState=PRESS_DOWN;
						}
					}
				break;	
					
				case PRESS_UP:
					if(getkeyTick()>=ShortPress && getkeyTick()<=LongPress)
					{
						if(exKeyValueFlag == 0)
						{
//							printf("-> ShortPress <-\r\n");
							tempKeyValue=lastKeyValue;
						}
						else
						{
							lastKeyValue=0;
						}
					}
					else
					{
						lastKeyValue=0;
					}
		//			printf("-> Press UP Tiem :%lu <-\r\n",getkeyTick());
					clearkeyTick();
					keyState= NONE_PRESS;
				break;
					
				case LONG_PRESS_START:
					if(getkeyTick() > LongPress)
					{
						//	printf("-> LongPress <-\r\n");
							tempKeyValue=lastKeyValue+keyLoneStep;
						//	printf("-> Press UP Tiem :%lu <-\r\n",getkeyTick());
							clearkeyTick();
							keyState= LONG_PRESS_START;
					}
					else if(exKeyValueFlag == 0)
					{
					//	printf("-> LongPress up<-\r\n");
						keyState= NONE_PRESS;
					}
				break;
					
				default:
				{
					keyState=NONE_PRESS;
				}
				break; 						
		} 
		return tempKeyValue;
}

uint32_t selectTouchKeyenent(uint32_t evt)
{
	uint32_t keyValueTemp = 0;
	
	switch(evt)
	{	
			case PowerKey:{
					keyValueTemp = BTN_ONOFF_EVENT;
				//	printf("keyvalue ->1\r\n");
			}
			break;
			
			case ModeKey:{
					keyValueTemp = BTN_MODE_EVENT;
					//keyValueTemp = BTN_TIM_EVENT;
				//	printf("keyvalue ->2\r\n");
			}
			break; 
			
			case IonKey:{
					//keyValueTemp = BTN_MODE_EVENT;
					keyValueTemp = BTN_ION_EVENT;
					//printf("keyvalue ->3\r\n");
			}
			break;
			
			case AmdKey:{
					keyValueTemp = BTN_AMD_EVENT;
					//printf("keyvalue ->4\r\n");
			}
			break;
			
			case ModeLoneKey:{
				keyValueTemp = BTN_FILTER_RESET_EVENT;
				// evt_Filter=eReset;
				//printf("keyvalue ->5\r\n");
			}
			break;
			
			case FactoryTestKey:{
					keyValueTemp = BTN_FACTORY_TEST_MODE_EVENT;
				//	printf("keyvalue ->6\r\n");
			}
			break;

			case FilterSpeedOutKey:{
					keyValueTemp = BTN_FILTER_SPEED_UP_EVENT;
					//printf("keyvalue ->7\r\n");
			}
			break;
			
			case MatchNetworkKey:{
					keyValueTemp = BTN_MATCH_NETWORK_EVENT;
					//printf("keyvalue ->8\r\n");
			}
			break;
			
			default:
			{
					keyValueTemp = 0x00;
			}
			break; 			
	}
	return keyValueTemp; 
}

void touchKeyScanHandle(void)
{
    uint32_t xdata keyValue = 0, eventTemp = 0;
    if(SOCAPI_TouchKeyStatus&0x80)	    
    {	   																	
        SOCAPI_TouchKeyStatus &= 0x7f;
        keyValue = TouchKeyScan();
		keyValue = getTouchKeyValue(keyValue);
        //eventValue = selectTouchKeyenent(keyValue);
		eventTemp = selectTouchKeyenent(keyValue);
		if(eventTemp < 0x200)
			eventValue = eventTemp;
        TouchKeyRestart();		 			
    }		
}  

